package com.householditemtwo.admin.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.householditemtwo.admin.dao.LyDao;
import com.householditemtwo.common.entity.Ly;
import com.householditemtwo.common.entity.Product;
import com.householditemtwo.common.utils.BaseServlet;
import com.householditemtwo.common.utils.Result;
import com.mysql.jdbc.StringUtils;

/**
 * 后台产品Servlet 
 */
@WebServlet("/admin/ly")
public class LyServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	//创建dao
	LyDao dao=new LyDao();
	
	
	/**
	 * 产品列表查询所有的产品
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//接收页面传的条件查询数据
		String proname=request.getParameter("lyname");
		
		//接收页面传过来的分页值
		//当前是第几页，如果没传就默认第一页
		int page=StringUtils.isNullOrEmpty(request.getParameter("page"))? 1 : Integer.parseInt(request.getParameter("page"));
		//每页显示几条
		int pageSize=StringUtils.isNullOrEmpty(request.getParameter("limit"))? 1 : Integer.parseInt(request.getParameter("limit"));
		
		//调用dao
		int total=dao.count(proname);
		List<Ly> list=dao.selectAllProduct(page,pageSize,proname);
		//把数据传到页面(必须传给也页面json格式的数据)
		PrintWriter out=response.getWriter();
		out.write(Result.toClient(0, "成功", total, list));
		out.flush();
		out.close();
	}
	
	
	/**删除产品
	 * 修改cahnpin
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//接收页面传的产品详情，并且封装到实体类里
		int id=	Integer.parseInt(request.getParameter("lyid"));
		
		
		
		//调用DAO
		int res=dao.delete(id);
		
		
		//把数据传到页面(必须传给也页面json格式的数据)
		PrintWriter out=response.getWriter();
		out.write(Result.toClient(res>0 ? 0 : 1,res>0 ? "修改成功" : "修改失败", null, null));
		out.flush();
		out.close();
		
	
	
	
	}
}
